package com.yidu.ladder.system.service.impl;

import java.util.List;

import org.springframework.stereotype.Service;

import com.yidu.ladder.system.dao.ResourceDao;
import com.yidu.ladder.system.domain.Resource;
import com.yidu.ladder.system.domain.ResourceVo;
import com.yidu.ladder.system.service.ResourceService;
import com.yidu.ladder.util.IDGenerator;


/**
 * 资源业务逻辑实现类
 * @author ld
 *
 */
@Service
public class ResourceServiceImpl implements ResourceService {
	
	/** 关联数据访问层 */
	@javax.annotation.Resource
	private ResourceDao resourceDao;
	
	@Override
	public List<ResourceVo> findResourceSelective (ResourceVo resourceVo) {
		//查询资源根据条件
		return resourceDao.findResourceSelective(resourceVo);
	}
	
	@Override
	public List<ResourceVo> queryAllChildResource(String resourceId) {
		//根据资源ID查询子资源
		List<ResourceVo> resourceVoList = resourceDao.selectResourceByParenntId(resourceId);
		//如果该资源下有子资源
		if (resourceVoList.size() != 0) {
			//遍历子资源
			for (ResourceVo resourceVo : resourceVoList) {
				//当前资源的ID
				String resId = resourceVo.getResourceId();
				//查询这个资源是否有子资源
				List<ResourceVo> childResourceVoList = resourceDao.selectResourceByParenntId(resId);
				//如果当前子资源集合不为空
				if (childResourceVoList.size() != 0) {
					//标识有直接子资源
					resourceVo.setHaveChild(true);
				}
			}
		}
		//返回资源集合
		return resourceVoList;
	}
	
	@Override
	public boolean addResource(Resource resource) {
		//随机生成ID
		resource.setResourceId(IDGenerator.getId());
		//调用添加资源
		return resourceDao.addResource(resource) == 1 ? true :false;
	}

	@Override
	public boolean removeResource(String[] resourceIds) {
		//根据资源ID删除资源
		int result = resourceDao.deleteResourceById(resourceIds);
		return result != resourceIds.length ? true : false;
	}
	
	@Override
	public boolean updateResource(Resource resource) {
		//调用修改资源
		int result = resourceDao.updateResource(resource);
		return result == 1 ? true : false;
	}
	
}
